'use strict';

const Service = require('egg').Service;

class CommonService extends Service {
  async refreshToken() {
    const { ctx } = this;
    try {
      const token = ctx.request.header.authorization;
      const info = ctx.helper.analysisToken(token);
      const { data } = info;
      const newToken = ctx.helper.generateToken(data, 60 * 1000 * 24);
      ctx.service.cache.set(data, newToken, 60 * 60);
      const msg = '';
      const res = newToken;
      return ctx.helper.body.SUCCESS({ ctx, msg, res });
    } catch (e) {
      ctx.logger.error('重新生成token失败', e);
      const msg = '用户信息获取失败';
      return ctx.helper.body.ERROR({ ctx, msg });
    }
  }
}

module.exports = CommonService;
